<?php
/**
 * @category   Delcampe
 * @pack
 * age    Delcampe_Resource
 **/
class Delcampe_Resource {
	private $_db; 
	
	public function __construct() 
  { 
  	$this->_db = Zend_Registry::get('db'); 
  } 
  
	public function addRelResourceUser($resource_id, $member_id, $role_id ){
		$data = array(
			'member_id' 	=> $member_id,
    	'resource_id' => $resource_id, 
    	'role_id' 		=> $role_id);
  	return $this->_db->insert('rel_resource_user',$data);     	
	}
	
    public function incrementViewCount($resource_id, $resource_type_id){
      $this->_db->exec('UPDATE resource SET hits = hits + 1 WHERE id='.$resource_id.' AND resource_type_id='.$resource_type_id);
      return true;
    }
	
	public function updateResource($resource_id, $resource_type_id, $commentable, $update_date){
		$data = array(
			'last_update' =>$update_date, 
			'commentable' => $commentable);
        return $this->_db->update('resource',$data,'id='.$resource_id.' AND resource_type_id='.$resource_type_id);    
	}
	
	public function addResource($resource_id, $resource_type_id, $mime_type, $commentable, $created_date){
		$data = array(
			'id' =>$resource_id, 
			'resource_type_id' => $resource_type_id,
			'mime_type' => $mime_type,
			'commentable' => $commentable,
			'created_date' => $created_date);
        return $this->_db->insert('resource',$data);    
	}
	
	public function deleteResource($resource_id,$res_type_id){
		 $delete_blog_post = $this->_db->exec('DELETE FROM resource
						   WHERE id ='.$resource_id.' AND resource_type_id ='.$res_type_id);
	}
	
	public function getResourceTypeId($resource_text){	
	  $obj = $this->_db->select()
	          ->from('resource_types')
	          ->where("name = '$resource_text'");
	  $getResources = $this->_db->fetchRow($obj);
	  return $getResources['resource_type_id'];
	}
	
	public function getApplicationId($application_name) {
		$getApplicationId = $this->_db->fetchRow(
    	'SELECT id 
		  FROM application 
		  WHERE name = ?', array($application_name));	
		return $getApplicationId['id'];
	}

	public function getResourceById($resourceid)
	{		
		Zend_Loader::loadFile('../application/blog/models/BlogPost.php');
		try {
			$resourceType = $this->_db->fetchRow(
				"SELECT rt.name
				 FROM resource_types rt
				 LEFT JOIN resource r ON r.resource_type_id = rt.resource_type_id
				 WHERE r.id = ?", array($resourceid)
			);
		
			foreach ($resourceType as $resourceName)
			{
				switch($resourceName) 
				{
					case 'blog/post' :					
						$blogobj = new Blog_Post();
				    $blog_post_entries = $blogobj->getBlogPost($resourceid);
	    			$comment_title = $blog_post_entries['post_title'];
	    			return $comment_title;
						break;
				}
			}
		}
		catch (Exception $e)
		{
			return null;
		}		
	}
	
	/**
   * Check whether the comment is commentable or non-commentable
   *
   * @param int $resid
   * @return array
   */
  
  public function is_commentable($resid)
  {
  	$resultSet = $this->_db->fetchRow("
      SELECT r.commentable
      FROM resource r
      WHERE r.id = ?", array($resid));
  	return $resultSet;
  }
  
  /**
   * fetch all resource types
   * 
   * @return array
   */
  
  public function getResourceTypes()
  {
  	$resultSet = $this->_db->fetchAll("
  		SELECT rt.name, rt.resource_type_id
			FROM resource_types rt
  	");
  	
  	return $resultSet;
  }
  
  /**
   * get member details by member id
   *
   * @param int $member_id
   * @return array
   */
  
  public function getMemberDetailsById($member_info)
  {
  	try{
 				return $this->_db->fetchAll("
	 				SELECT m.firstname, m.email
	 				FROM members m
	 				WHERE m.id_member IN (" . implode(',',$member_info). ")");
		} 
 		catch (Exception $e)
 		{
 			return null;
 		}
  }
}